<template>
	<view class="Multiple">
		<button @click="selectFile">选择Excel录入</button>
    <book-loading :visible.sync="isUploading"/>
	</view>
</template>

<script>
	import BookLoading from "../../components/bookLoading";
  export default {
    components: {BookLoading},
    data() {
			return {
				isUploading: false
			};
		},
    mounted() {
      uni.$on('apiShowLoading',()=>{
        this.isUploading = true
      })
      uni.$on('apiHideLoading',()=>{
        this.isUploading = false
      })
    },
		methods:{
			selectFile(){
				this.$utils[this.$utils.system.platform()].selectLocalFiles((res)=>{
					console.log(res);

					// plus.io.resolveLocalFileSystemURL(res,(result)=>{
					// 	console.log('result',result.toLocalURL());
					// },(err)=>{
					// 	console.log('er',err);
					// })

					if (/^xlsx?$/.test(res.split('.')[1])) {

						// uni.showLoading({
						// 	title:'请稍后'
						// })
            this.isUploading = true
						const uploadTask = uni.uploadFile({
							url: 'https://huntwolf.top/book/batch', //仅为示例，非真实的接口地址
							filePath: "file://" + res,
							header:{
								//请求头需要token时再放
								'Accept': 'application/json',
								'Content-Type': 'multipart/form-data',
								'token':uni.getStorageSync('userLoginRes')?.token || ''
							},
							name: 'file',
							success: (uploadFileRes) => {
								console.log(uploadFileRes.data)
							}
						});

						uploadTask.onProgressUpdate((res) => {
						    console.log('上传进度' + res.progress);

						    // 测试条件，取消上传任务。
						    if (res.progress == 100) {
						        // uni.hideLoading();
                  this.isUploading = false
						    }
						});

					} else {
						uni.showModal({
							title:'提示',
							content:'请选择excel文档！',
							showCancel:false,
						})
					}

				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.Multiple{
		position: absolute;
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;

	}
</style>
